package com.xhs.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.xhs.config.SystemConfig;


/**
 * @description:
 * @author: Feri(邢朋辉)
 * @time: 2021/10/15 11:34
 */
public class AliSmsUtils {
    public static Client client;
    static {
        //创建配置对象，设置密钥
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(SystemConfig.ALI_KEY)
                // 您的AccessKey Secret
                .setAccessKeySecret(SystemConfig.ALI_KEYSECRET);
        //实例化客户端对象
        try {
            client=new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送验证码
     * @param param json格式参数
     * @param sign 签名
     * @param temcode 模板code码
     * @param phone 手机号*/
    public static boolean sendCodeSms(String sign,String temcode,String phone,String param){
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(sign)
                .setTemplateCode(temcode)
                .setTemplateParam(param)
                .setPhoneNumbers(phone);
        // 复制代码运行请自行打印 API 的返回值
        try {
            SendSmsResponse response=client.sendSms(sendSmsRequest);
            return response.getBody().code.equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**注册验证码*/
    public static boolean sendRegisterCode(String phone,int code){
        return sendCodeSms(SystemConfig.ALI_SMSSIGN,SystemConfig.ALI_SMSCODE_REGISTER,phone,"{\"code\":"+code+"}");
    }
    /**登陆验证码*/
    public static boolean sendLoginCode(String phone,int code){
        return sendCodeSms(SystemConfig.ALI_SMSSIGN,SystemConfig.ALI_SMSCODE_LOGIN,phone,"{\"code\":"+code+"}");
    }
    /**密码找回验证码*/
    public static boolean sendFindCode(String phone,int code){
        return sendCodeSms(SystemConfig.ALI_SMSSIGN,SystemConfig.ALI_SMSCODE_FIND,phone,"{\"code\":"+code+"}");
    }


}